<?php

class Gallery extends BaseGallery
{
	
public function __toString() {
		return $this->getPhotoPath();
	}
	
	 public function delete(PropelPDO $con = null)
  {
  	  ///Удаляем картинки
  	 if($this->getPhotoPath()!='')
  	 {
	  	$fname = sfConfig::get('sf_upload_dir').'/users/gallery/'.$this->getPhotoPath();
	    $fthname = sfConfig::get('sf_upload_dir').'/users/gallery/thumbnail/'.$this->getPhotoPath();
	    $_fthname = sfConfig::get('sf_upload_dir').'/users/gallery/_thumbnail/'.$this->getPhotoPath();

	    
	    @unlink($fname);
	    @unlink($fthname);
	    @unlink($_fthname);
  	 }
  	 
  	    $gr = GroupgalleryPeer::retrieveByPK($this->getGroupId());
		$c = new Criteria();
		$c->add(GalleryPeer::GROUP_ID,$this->getGroupId());		
		$cnt = GalleryPeer::doCount($c);
		$gr->setCount($cnt-1);
		$gr->save();
  	 
  	 parent::delete();
  }
	
  
  public function save(PropelPDO $con = null)
	{
		parent::save();
		
		$gr = GroupgalleryPeer::retrieveByPK($this->getGroupId());
		$c = new Criteria();
		$c->add(GalleryPeer::GROUP_ID,$this->getGroupId());		
		$cnt = GalleryPeer::doCount($c);
		$gr->setCount($cnt);
		$gr->save();
		
		////Задаем флаг присутствия фоток
		$pf = $this->getsfGuardUser();
		$c = new Criteria(); 
		$c->add(GroupgalleryPeer::USER_ID,$pf->getId());
		$c->add(GroupgalleryPeer::PUB,3);
		$cnt = GroupgalleryPeer::doCount($c);
		if($cnt>0)
		{
			$pf->setIsPhoto(true);
			$pf->save();
		}
		////
		
	}
	
}
